<%--
  Created by IntelliJ IDEA.
  User: 14258
  Date: 2020/9/8
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>ajax</title>
    <script type="text/javascript">
      function test1() {
        //1.创建ajax对象
        var xmlHttp;
        if (window.XMLHttpRequest){
          //非ie浏览器
          xmlHttp = new XMLHttpRequest();
        }else if (window.ActiveXObject){
          //ie浏览器
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
        }
        //2.打开连接
        var username = document.getElementById("username").value;

        //get请求
        // xmlHttp.open("get","/test?username="+username,true);
        //post请求
        xmlHttp.open("post","/test",true);

        //3.指定回调函数
        xmlHttp.onreadystatechange = function () {
          //3.1判断状态
          if (xmlHttp.readyState == 4){
            //3.2成功则接收响信息
            var text = xmlHttp.responseText;
            document.getElementById("rs").innerText = text;
          }
        }
        //get发送请求
        // xmlHttp.send(null);

        //post发送请求
        //post需要增加time随机参数，防止读取缓存
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        xmlHttp.send("username" + username);
      }
    </script>
  </head>
  <body>
  请输入用户名：<input type="text" id="username" name="" onblur="test1()"><span id="rs"></span>
  </body>
</html>
